<template>
  <div>
    <!-- 查询表单 -->
    <div class="check-form-box">
      <el-form
        :inline="true"
        :model="searchModel.data"
        class="demo-form-inline"
      >
        <el-form-item label="日期">
          <el-date-picker
            v-model="searchModel.createDate"
            type="daterange"
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item label="通行单编号">
          <el-input
            v-model.trim="searchModel.passNo"
            placeholder="请输入通行单编号"
          />
        </el-form-item>
        <el-form-item label="数据来源">
          <el-select v-model="searchModel.dataSrc" placeholder="请选择数据来源">
            <el-option label="全部" :value="null" />
            <el-option label="支付宝中视" value="1" />
            <el-option label="微信金瑜" value="2" />
            <el-option label="蜜蜂" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="用户ID">
          <el-input
            v-model.trim="searchModel.userId"
            :formatter="(value) => value.replace(/[^\d]/g, '')"
            placeholder="请输入用户ID"
          />
        </el-form-item>
        <el-form-item label="车牌号">
          <el-input
            v-model.trim="searchModel.carNo"
            placeholder="请输入车牌号"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" color="#ea5b24" @click="searchOrder()"
            >查询</el-button
          >
          <el-button
            type="primary"
            color="#ea5b24"
            @click="exportOrder()"
            v-if="pinia.isShow[83]"
            >导出Excel</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <!-- 列表 -->
    <div>
      <el-table :data="tableData.data" style="width: 100%">
        <el-table-column
          prop="gmtCreate"
          width="160"
          :formatter="rejigTime"
          label="创建时间"
        />
        <el-table-column prop="passNo" width="160" label="通行编号" />
        <el-table-column prop="orderSn" width="160" label="业务订单号" />
        <el-table-column prop="passDate" width="160" label="通行日期" />
        <el-table-column prop="passMobile" width="160" label="手机号" />
        <el-table-column
          prop="dataSrc"
          :formatter="judgeDataSrc"
          label="数据来源"
        />
        <el-table-column prop="userId" width="150" label="用户ID" />
        <el-table-column prop="carNo" label="车牌号" />
        <el-table-column prop="passPrice" label="通行金额" />
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            {{ scope.row.status == 0 ? "核销失败" : "核销成功" }}
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页插件 -->
      <div style="text-align: center">
        <el-pagination
          :total="totalNum"
          @current-change="handleCurrentChange"
          background
          :page-size="size"
          layout="prev, pager, next, total"
          :current-page="page"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { findPassOrderApi, findPassExcelApi } from "../../../api/index";
import { formatDateTime } from "../../../utils/formatDateTime";
import { usePermissStore } from "../../../store/permiss";
const pinia = usePermissStore();

// 分页列表数据
let tableData: any = reactive([]);
const totalNum = ref(0);
const page = ref(1);
const size = 8;

// 翻页
let flipData = reactive({
  page: page,
  size: size,
});
const handleCurrentChange = (val: number) => {
  page.value = val;
  flipData.page = page.value;
  findOrder(flipData);
};

// 搜索模型
const searchModel: any = reactive({
  passNo: null,
  dataSrc: null,
  userId: null,
  carNo: null,
  createDate: null,
  startTime: null,
  endTime: null,
  page: page,
  size: size,
});

// 通行参数
const refundParam = reactive({
  refundTableId: null,
  reason: null,
});

const judgeDataSrc = (row) => {
  switch (row.dataSrc) {
    case 1:
      return "支付宝中视";
    case 2:
      return "微信金瑜";
    case 3:
      return "蜜蜂";
    default:
      return "";
  }
};

//处理时间格式
const rejigTime = (row) => {
  let strTime = "";
  row.gmtCreate == null ? "" : (strTime = row.gmtCreate.replace(/T/g, " "));
  return strTime;
};

// 搜索通行订单
const searchOrder = () => {
  page.value = 1;
  if (searchModel.createDate) {
    searchModel.startTime = formatDateTime(searchModel.createDate[0]);
    searchModel.endTime = formatDateTime(searchModel.createDate[1]).replace(
      "00:00:00",
      "23:59:59"
    );
  } else {
    searchModel.startTime = null;
    searchModel.endTime = null;
  }
  searchParmSearch();
  findOrder(searchModel);
  flipData = JSON.parse(JSON.stringify(searchModel));
};

// 查询通行订单
const findOrder = (searchModel) => {
  findPassOrderApi(searchModel).then((res) => {
    if (res.data.code === 2000) {
      tableData.data = res.data.data.data;
      totalNum.value = res.data.data.total;
    } else {
      ElMessage.error("查询失败");
    }
  });
};

// 搜索参数检查
const searchParmSearch = () => {
  Object.keys(searchModel).forEach((key) => {
    if (!searchModel[key]) {
      searchModel[key] = null;
    }
  });
};

//导出订单
const exportOrder = () => {
  findPassExcelApi(searchModel).then((res) => {
    const blob = new Blob([res.data]);
    const fileName = "通行订单数据.xls";
    const elink = document.createElement("a"); // 创建a标签
    elink.download = fileName; // 为a标签添加download属性
    elink.style.display = "none";
    elink.href = URL.createObjectURL(blob);
    document.body.appendChild(elink);
    elink.click(); // 点击下载
    URL.revokeObjectURL(elink.href); // 释放URL 对象
    document.body.removeChild(elink); // 释放标签
  });
};

onMounted(() => {
  findOrder(searchModel);
});
</script>

<style scoped>
:deep(.demo-form-inline) {
  display: grid;
  justify-items: stretch;
  grid-template-columns: repeat(auto-fill, 25%);
}

:deep(.el-select) {
  width: 100%;
}

:deep(.el-form-item) {
  width: 90%;
}
</style>
